package com.dongdongshop.front.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.front.pojo.TbUser;
import com.dongdongshop.front.service.UserService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import com.dongdongshop_util.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


@Controller
@RequestMapping("register")
public class RegisterController {

    private Logger logger= LoggerFactory.getLogger(RegisterController.class);

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Reference(check = false)
    private UserService userService;

    @RequestMapping("toRegister")
    public String toRegister(){
        return "admin/register";
    }

    @ResponseBody
    @RequestMapping("duanXin")
    public void duanXin(String phone){
        //通过mq发送
        /*rocketMQTemplate.convertAndSend("phone",phone);
        System.out.println("mq发送成功"+phone);*/

        try {
            userService.sendVerify(phone);
        }catch (Exception e){
            logger.error("错误为:{}{}{}",e);
        }
    }

    @ResponseBody
    @RequestMapping("zhuCe")
    public int zhuCe(TbUser tbUser, String number){
        try {
            //判断用户是否填写账号密码
            if(tbUser.getUsername() == null  || "".equals(tbUser.getUsername())){
                return 1;
            }else if(tbUser.getPassword() == null  || "".equals(tbUser.getPassword())){
                return 2;
            }
            //判断用户是否存在
            TbUser user = userService.getUserByName(tbUser.getUsername());
            if (user != null){
                return 3;
            }
            //判断验证码是否正确 存到redis的key是手机号所以通过手机号来取出验证码
            String redisNumber = (String) redisTemplate.boundHashOps("User").get(tbUser.getPhone());
            if (!redisNumber.equals(number)){
                return 4;
            }
            //等于null执行添加
            if (user == null){
                tbUser.setSalt("90aa5a");
                String password = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), "90aa5a", 10);
                tbUser.setPassword(password);
                tbUser.setStatus("0");
                tbUser.setUpdated(new Date());
                tbUser.setCreated(new Date());
                userService.addUser(tbUser);
            }
            return 5;
        }catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return 6;
        }
    }

    /* 校验账号是否存在 */
    @RequestMapping("checkUserName")
    @ResponseBody
    public Result checkUserName(String userName){
        try {

            TbUser user  = userService.getUserByName(userName);
            return Result.success(ResultEnum.SUCCESS,user);
        }catch (Exception e){

            logger.error(String.valueOf(ResultEnum.ERROR),e);

            return Result.response(ResultEnum.ERROR);
        }
    }

    /* 判断验证码是否存在或超时 */
    @RequestMapping("getVerify")
    @ResponseBody
    public Result getVerify(String telphone){
        try {
            if (StringUtils.isNoneBlank(telphone)){
                if (redisTemplate.hasKey(telphone)){
                    String verify = redisTemplate.boundValueOps(telphone).get().toString();
                    if (StringUtils.isNoneBlank(verify)){
                        /*System.err.println("系統生成的驗證碼為："+verify);
                        System.err.println("用戶輸入的手機號為："+telphone);*/
                        return Result.success(ResultEnum.SUCCESS,verify);
                    }else {
                        return  Result.success(ResultEnum.SUCCESS,null);
                    }
                }else {
                    return  Result.success(ResultEnum.SUCCESS,null);
                }

            }else {
                return  Result.success(ResultEnum.SUCCESS,null);
            }

        }catch (Exception e){
            logger.error(String.valueOf(ResultEnum.ERROR),e);

            return Result.response(ResultEnum.ERROR);
        }

    }

    /* 重置密碼 */
    @RequestMapping("resetPwd")
    @ResponseBody
    public Result resetPwd(Long userId, String mail){
        try {

            userService.resetPwd(userId,mail);
            return  Result.perfect(ResultEnum.SUCCESS);
        }catch (Exception e){

            logger.error(String.valueOf(ResultEnum.ERROR),e);

            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("toHomeIndex")
    public String toHomeIndex(){
        return "admin/home-index";
    }

    @RequestMapping("toResetPwd")
    public String toResetPwd(){
        return "admin/resetPassword";
    }

}
